package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    public void addShopping(ShoppingCartDTO shoppingCartDTO) {
        //需求分析：尤其注意，购物车的用户，都是在其登录时，就已经获取到了
        //先判断当前商品是否已存在，如果存在，则只需要在原有的数量上+1 注意：只有在商品名称、口味也一样时，才算是相同
        //如果没有才需要将商品加上购物车
        //1、先查询购物车该用户是否已经添加了该商品
        //获取当前用户ID
        Long userId = BaseContext.getCurrentId(); //获取当前用户，所添加的该商品是否已存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);//就封装了所需要的数据
        List<ShoppingCart> list = shoppingCartMapper.selCart(shoppingCart);//最多有且只有一个,根据用户id，菜品、套餐和口味查询
        if (list != null && list.size() > 0) {
//            Integer number = list.get(0).getNumber();
            list.get(0).setNumber(list.get(0).getNumber() + 1);
            shoppingCartMapper.updateCart(list.get(0));
        } else {
            //当数据为新增的时候，就需要吧购物车添加的商品其他信息补全，如：图片、价格、菜品名称等等，
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //不为空则确定商品是菜品 ，查出当前菜品信息
                Dish byDishId = dishMapper.getByDishId(dishId);
                shoppingCart.setAmount(byDishId.getPrice());
                shoppingCart.setImage(byDishId.getImage());
                shoppingCart.setName(byDishId.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

            } else {
                Setmeal setmealById = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmealById.getPrice());
                shoppingCart.setImage(setmealById.getImage());
                shoppingCart.setName(setmealById.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            shoppingCartMapper.addCart(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> showShopCart() {
        Long userId = BaseContext.getCurrentId();//获取当前用户id，查询

        List<ShoppingCart> list = shoppingCartMapper.showGetByID(userId);
        return list;
    }

    @Override
    public void clean() {
        //根据用户id清楚购物车
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void subShop(ShoppingCartDTO shoppingCartDTO) {
        //思路分析：可以判断减少的商品数量是否为1份，如果是，那么直接删除掉该商品即可，如果大于1，则在基础上减掉一份
        //同样需要先获取到当前用户的userId
        Long userId = BaseContext.getCurrentId();
        //查询的是购物车的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);//将4个字段都封装给购物车进行查询操作

        //查出来的数据判断=1 ？大于1
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selCart(shoppingCart);

        if (shoppingCartList.get(0).getNumber()==1){

                    log.info("用户id{}",shoppingCartList.get(0).getUserId());
            //==1：直接删掉
            shoppingCartMapper.deleteByDishOrSetmealId(shoppingCartList.get(0));
        }else {
            //原基础上的number-1即可
            Integer number=shoppingCartList.get(0).getNumber()-1;
            log.info("number:{}",number);
            shoppingCartList.get(0).setNumber(number);
            shoppingCartMapper.updateCart(shoppingCartList.get(0));
        }


    }
}
